using B.S.Domain.InvoiceManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.B.S.KApplications.Command.InvoiceCommand
{
    /// <summary>
    /// 删除销售发票命令处理程序
    /// 处理销售发票的删除逻辑
    /// </summary>
    public class DeleteSalesInvoiceCommandHandler : IRequestHandler<DeleteSalesInvoiceCommand, APIRequst<bool>>
    {
        private readonly IBaseRepository<SalesInvoice> _salesInvoiceRepository;
        private readonly IBaseRepository<SalesInvoiceDetail> _salesInvoiceDetailRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="salesInvoiceRepository">销售发票仓储</param>
        /// <param name="salesInvoiceDetailRepository">销售发票明细仓储</param>
        public DeleteSalesInvoiceCommandHandler(IBaseRepository<SalesInvoice> salesInvoiceRepository, IBaseRepository<SalesInvoiceDetail> salesInvoiceDetailRepository)
        {
            _salesInvoiceRepository = salesInvoiceRepository;
            _salesInvoiceDetailRepository = salesInvoiceDetailRepository;
        }

        /// <summary>
        /// 处理删除销售发票的命令
        /// </summary>
        /// <param name="request">删除销售发票命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处理结果</returns>
        public async Task<APIRequst<bool>> Handle(DeleteSalesInvoiceCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 验证参数
                if (request.InvoiceId <= 0)
                {
                    return new APIRequst<bool> { Code=RequstCode.失败, Msg = "发票ID不能为空" };
                }

                // 查询发票信息
                var salesInvoice = await _salesInvoiceRepository.GetAll().FirstOrDefaultAsync(a=>a.InvoiceId== request.InvoiceId);
                if (salesInvoice == null || salesInvoice.isDel)
                {
                    return new APIRequst<bool> { Code = RequstCode.失败, Msg = "发票不存在或已被删除" };
                }

                // 执行软删除
                salesInvoice.isDel = true;
                salesInvoice.UpdateTime = DateTime.Now;
                salesInvoice.DeleteName = request.DeleteName;
                salesInvoice.DeleteTime = DateTime.Now;

                // 保存发票主表更新
                await _salesInvoiceRepository.Update(salesInvoice);

                // 软删除发票明细
                var invoiceDetails = await _salesInvoiceDetailRepository.GetAll().Where(d => d.InvoiceId == request.InvoiceId && !d.isDel).ToListAsync();
                foreach (var detail in invoiceDetails)
                {
                    detail.isDel = true;
                    detail.UpdateTime = DateTime.Now;
                    await _salesInvoiceDetailRepository.Update(detail);
                }

                return new APIRequst<bool> { Code = RequstCode.成功, Data = true, Msg = "删除成功" };
            }
            catch (Exception ex)
            {
                // 记录异常并返回失败结果
                return new APIRequst<bool> { Code = RequstCode.失败, Msg = $"删除失败：{ex.Message}" };
            }
        }
    }
}